/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package predictor.gui.dataviewer;

import java.awt.Color;
import java.awt.Component;
import java.util.Enumeration;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableModel;
import weka.core.Attribute;
import weka.core.Instances;
import weka.gui.JTableHelper;

/**
 *
 * @author Administrator
 */
public class DataTableView extends BaseTableView {

    private int m_minRows;
    private int m_minCols;

    public DataTableView(int r, int c) {
        super(r, c);
        m_minRows = r;
        m_minCols = c;

    }

    @Override
    public void setInstances(Instances data) {
        DataTableModel model = new DataTableModel(m_minRows, m_minCols);
        model.setInstances(data);
        m_tableContent.setModel(model);
        super.setInstances(data);
        for (int k = 0; k < m_tableContent.getColumnCount(); k++) {
            m_tableContent.getColumnModel().getColumn(k).setCellRenderer(new PredCellRenderer());
        }

    }

    @Override
    protected void adjustLayout() {
        DataTableModel tableModel;
        int i;
        JComboBox combo;
        Enumeration enm;

        tableModel = (DataTableModel) m_tableContent.getModel();

        for (i = 0; i < m_tableContent.getColumnCount(); i++) {
            // optimal colwidths (only according to header!)
            JTableHelper.setOptimalHeaderWidth(m_tableContent, i);

            if (i >= 0 && i < tableModel.getM_numAttributes()) {
                if (tableModel.getType(i) == Attribute.NOMINAL) {
                    combo = new JComboBox();
                    combo.addItem(null);
                    enm = tableModel.getInstances().attribute(i).enumerateValues();
                    while (enm.hasMoreElements()) {
                        combo.addItem(enm.nextElement());
                    }
                    m_tableContent.getColumnModel().getColumn(i).setCellEditor(new DefaultCellEditor(combo));
                } else {
                    m_tableContent.getColumnModel().getColumn(i).setCellEditor(null);
                }
            }
        }
    }

    @Override
    protected TableModel createTableModel(int r, int c) {
        return new DataTableModel(r, c);
    }

    @Override
    protected void setLayout() {
        this.setMargin(2, 2);
        this.setCellSize(80, 23);
    }

    //若i，j两列某行预测结果与实际结果不一致，则高亮显示该行
    public void setErrorRow(int i, int j) {
        DataTableModel model = (DataTableModel) m_tableContent.getModel();
        int numAtt = model.getM_numAttributes();
        if (i >= numAtt || j >= numAtt) {
            return;
        }
        for (int k = 0; k < m_tableContent.getColumnCount(); k++) {
            m_tableContent.getColumnModel().getColumn(k).setCellRenderer(new PredCellRenderer());
        }
//        m_tableContent.setDefaultRenderer(null, new PredCellRenderer(i, j));
    }

    private DataTableModel getTableModel() {
        return (DataTableModel) m_tableContent.getModel();
    }

    private Instances getInstances() {
        return getTableModel().getInstances();
    }

    private class PredCellRenderer extends DefaultTableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            

            Instances data = getInstances();
            if (data != null && row < data.numInstances()) {
                int i = data.classIndex();
                int j = data.numAttributes() - 1;
                if (i != -1 && i != j) {
                    if (data.instance(row).value(i) != data.instance(row).value(j)) {
                        this.setBackground(Color.red);
                    }else{
                        this.setBackground(table.getBackground());
                    }
                }
            }
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            return this;
        }
    }
}
